﻿--[[
   @brief    model for group list ui
   @author   vincentlao
   @version  1.0
   @date     2015-06-11
--]]

group_list_model =
{
    MY_FAVOR_DISCUSSION_KEY = "self_favor_discussion";
    mini_mode_loaded = false;
    is_get_social_test_info = false;
    is_get_group_list_done = false;
    social_user_level = 0;
    social_invite_code_count = 0;

    group_table_old_ = {};
    group_table_new_ = {};
    group_table_ = {}; -- group_table_old_+group_table_new_
    has_get_unread_msg_count = false;
    ierd_tgp.tpf_ui.Require("social\\group_message\\model.lua");

    group_sort_func = function(a, b)
        if a.newly_create and a.newly_create == true and (b.newly_create == nil or b.newly_create == false) then
            return true;
        end

        if b.newly_create and b.newly_create == true and (a.newly_create == nil or a.newly_create == false) then
            return false;
        end

        if a.newly_create and a.newly_create == true and b.newly_create and b.newly_create == true then
            return a.create_time > b.create_time;
        end

        local a_last_msg_time = 0;
        local b_last_msg_time = 0;

        local a_message_count = 0;
        local b_message_count = 0;

        local a_create_time = 0;
        local b_create_time = 0;

        if a.history_unread_msg_count then
            a_message_count = a.history_unread_msg_count;
        end

        a_message_count = a_message_count;

        if b.history_unread_msg_count then
            b_message_count = b.history_unread_msg_count;
        end

        b_message_count = b_message_count;


        if a.create_time then
            a_create_time = a.create_time;
        end

        a_create_time = a_create_time;

        if b.create_time then
            b_create_time = b.create_time;
        end

        b_create_time = b_create_time;

        if a.last_msg_ then
            a_last_msg_time = a.last_msg_.time;
        end

        if b.last_msg_  then
            b_last_msg_time = b.last_msg_.time;
        end

        if (a_message_count > 0) and (b_message_count <= 0) then
            return true;
        end

        if (b_message_count > 0) and (a_message_count <= 0) then
            return false;
        end

        if a_last_msg_time ~= b_last_msg_time then
            return a_last_msg_time > b_last_msg_time;
        end

        return a_create_time > b_create_time;
    end;

    init = function()
        ierd_tgp.tpf_ui.Log("[group_list_model]init");
        ierd_tgp.tpf_ui.Require("social\\group_list\\group_list.lua");
        group_list_model.group_table_ = {};
        group_list_model.data_refreshed_ = nil;
    end;

    uninit = function()
        ierd_tgp.tpf_ui.Log("[group_list_model]uninit");
    end;
    
    sort = function()
        table.sort( group_list_model.group_table_, group_list_model.group_sort_func);
        group_list_model.data_refreshed_ = nil;
    end;

    get_group_list = function()
        return group_list_model.group_table_;
    end;

    modify_discussion_face_url = function()
        if type(group_list_model.group_table_) == "table" then
            for index, group in ipairs(group_list_model.group_table_) do
                if group.group_type == "MobileDiscus" then
                    group_list_model.group_table_[index].face_url = LoadStr("SOCIA_DISCUSS_FACE_URL");
                    group_list_model.data_refreshed_ = nil;
                end
            end
        end
    end;

    gen_group_list = function()
        group_list_model.group_table_ = {};

        if group_list_model.group_table_old_ == nil or type(group_list_model.group_table_old_) ~= "table" then
            group_list_model.group_table_old_ = {};
        end;

        if group_list_model.group_table_new_ == nil or type(group_list_model.group_table_new_) ~= "table" then
            group_list_model.group_table_new_ = {};
        end;

        for i,v in pairs(group_list_model.group_table_new_) do
            if v ~= nil then
                if group_list_model.is_exist(v.group_id) == false then
                    table.insert(group_list_model.group_table_,v)
                end
            end
        end

        for i,v in pairs(group_list_model.group_table_old_) do
            if v ~= nil then
                if group_list_model.is_exist(v.group_id) == false then
                    table.insert(group_list_model.group_table_,v)
                end
            end
        end
        group_list_model.check_list();
    end;

    set_group_list = function(group_list)
        if group_list == nil then
            group_list = {};
            ierd_tgp.tpf_ui.Log("[group_list_model]set_group_list:nil group list");
        end;
       group_list_model.group_table_old_ = group_list;
       group_list_model.gen_group_list();
       
       group_list_model.modify_discussion_face_url();
       group_list_model.sort();
       group_list_model.data_refreshed_ = nil;
       event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}");
    end;

    set_new_group_list = function(group_list)
        if group_list == nil then
            group_list = {};
            ierd_tgp.tpf_ui.Log("[group_list_model]set_group_list:nil group list");
        end;
        group_list_model.group_table_new_ = group_list.list;
        group_list_model.gen_group_list();
        group_list_model.modify_discussion_face_url();
        group_list_model.sort();
        event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}");
    end;

    on_broadcast_on_add_group = function(group) --新的IM
        group_list_model.add_group_info(1, group.info);
    end;

    on_broadcast_on_delete_group = function(group) --新的IM
        group_list_model.remove_group_info(group);
        local count_friend = friend_list_model.get_total_unread_msg_count();
        local count_group = group_list_model.get_total_unread_msg_count();
        local total = count_friend + count_group;

        local show_red_point = {};
        if total == 0 then
            show_red_point.show = "false";
            event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json(show_red_point));
        end

        local table_param = {};
        table_param["group_id"] = group.group_id;
        event_center.send_event(group_event.GROUP_CLEAR_MESSAGE,base_utility.table_to_json(table_param));
    end;

    is_newim_group = function(group_info)
        return true;
    end;

    is_newim_group_id = function(group_id)
        return true;
    end;

    is_newim_group_list = function(group_list)
        if group_list ~= nil and #group_list >= 1 then
            return group_list_model.is_newim_group(group_list[1]);
        end

        return true;
    end;

    change_info_in_newim = function(info)
        group_list_model.update_group_info_newim(info);
    end;

    on_broadcast_group_info_changed = function(group_list)
        if group_list == nil or group_list.list == nil then
            group_list = {};
            ierd_tgp.tpf_ui.Log("[group_list_model]set_group_list:nil group list");
        end;

        for index = 1, #group_list.list do
            group_list_model.change_info_in_newim(group_list.list[index]);-- 这个处理函数是由新的IM触发的
        end

        group_list_model.gen_group_list();
        group_list_model.modify_discussion_face_url();
        group_list_model.sort();
        event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}");
    end;

    is_exist = function(group_id)
        for index = 1, #group_list_model.group_table_ do
            if group_id == group_list_model.group_table_[index].group_id then
                return true;
            end
        end 
        return false;
    end;

    add_group_info = function(pos, group_info)
      if group_list_model.group_table_ == nil then
          group_list_model.group_table_ = {};
      end

        if group_list_model.is_newim_group(group_info) then
            table.insert(group_list_model.group_table_new_, pos, group_info);
        else
            table.insert(group_list_model.group_table_old_, pos, group_info);
        end
        group_list_model.gen_group_list();
       group_list_model.modify_discussion_face_url();
       group_list_model.data_refreshed_ = nil;
       event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}");
    end;
    
    remove_group_info = function(group_info)
        for index = #group_list_model.group_table_new_, 1, -1 do
            if group_info.group_id == group_list_model.group_table_new_[index].group_id then
                table.remove(group_list_model.group_table_new_, index);
                group_list_model.data_refreshed_ = nil;
                event_center.send_event(social_event.SOCIAL_REMOVE_GROUP_FROM_LIST, base_utility.table_to_json({group_id = group_info.group_id}));
            end
        end
        for index = #group_list_model.group_table_old_, 1, -1 do
            if group_info.group_id == group_list_model.group_table_old_[index].group_id then
                table.remove(group_list_model.group_table_old_, index);
                group_list_model.data_refreshed_ = nil;
                event_center.send_event(social_event.SOCIAL_REMOVE_GROUP_FROM_LIST, base_utility.table_to_json({group_id = group_info.group_id}));
            end
        end
        group_list_model.gen_group_list();
        group_list_model.modify_discussion_face_url();
        event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}");
    end;

    remove_group_info_by_id = function(group_id)
        for index = #group_list_model.group_table_new_, 1, -1 do
            if group_id == group_list_model.group_table_new_[index].group_id then
                table.remove(group_list_model.group_table_new_, index);
                group_list_model.data_refreshed_ = nil;
                group_list_model.check_list();
                event_center.send_event(social_event.SOCIAL_REMOVE_GROUP_FROM_LIST, base_utility.table_to_json({group_id = group_id}));
            end
        end
        for index = #group_list_model.group_table_old_, 1, -1 do
            if group_id == group_list_model.group_table_old_[index].group_id then
                table.remove(group_list_model.group_table_old_, index);
                group_list_model.data_refreshed_ = nil;
                group_list_model.check_list();
                event_center.send_event(social_event.SOCIAL_REMOVE_GROUP_FROM_LIST, base_utility.table_to_json({group_id = group_id}));
                break;
            end
        end
        group_list_model.gen_group_list();
        group_list_model.modify_discussion_face_url();
        event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}");
    end;

    update_group_notice = function(group_id, notice)
      for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
            group_list_model.group_table_[index]["notification"] = notice;
            group_list_model.data_refreshed_ = nil;
            group_list_model.check_list();
            break;
          end
      end 
    end;

    on_broadcast_mask_group_msg_inited = function (data_in)
        if data_in == nil then
            return;
        end
        for index = 1, #group_list_model.group_table_new_ do
            if data_in.id == group_list_model.group_table_new_[index].group_id then
                group_list_model.group_table_new_[index].mask_msg_flag = data_in.mask_msg_flag;
                group_list_model.data_refreshed_ = nil;

                if data_in.mask_msg_flag == 2 then
                    local table_param = {};
                    table_param["group_id"] = data_in.id;
                    event_center.send_event(group_event.GROUP_CLEAR_MESSAGE,base_utility.table_to_json(table_param));
                end

                --group update event
                event_center.send_event(group_event.GROUP_INFO_UPDATED,base_utility.table_to_json(group_info));
            end
        end
        group_list_model.gen_group_list();
        group_list_model.modify_discussion_face_url();
    end;

    -- 消息屏蔽选项（0：接收；1，拒绝）
    update_group_mask_msg_flag = function(group_id, flag)
        for index = 1, #group_list_model.group_table_old_ do
            if group_id == group_list_model.group_table_old_[index].group_id then
                group_list_model.group_table_old_[index]["mask_msg_flag"] = flag;
                group_list_model.data_refreshed_ = nil;
                group_list_model.check_list();
            end
        end
        for index = 1, #group_list_model.group_table_new_ do
            if group_id == group_list_model.group_table_new_[index].group_id then
                group_list_model.group_table_new_[index]["mask_msg_flag"] = flag;
                group_list_model.data_refreshed_ = nil;
                group_list_model.check_list();
            end
        end
        group_list_model.gen_group_list();
        group_list_model.modify_discussion_face_url();
        GroupBannerList:on_group_mask_update(group_id);
    end;

    update_group_info = function(group_info)
        if group_info == nil then
            return;
        end
        if group_list_model.is_newim_group(group_info) then
            group_list_model.update_group_info_newim(group_info);
        else
            group_list_model.update_group_info_oldim(group_info);
        end
        group_list_model.gen_group_list();
        group_list_model.modify_discussion_face_url();
    end;

    update_group_info_oldim = function(group_info)
        if group_info == nil then
            return;
        end
        for index = 1, #group_list_model.group_table_old_ do
            if group_info.group_id == group_list_model.group_table_old_[index].group_id then
                group_info.last_msg_ = group_list_model.group_table_old_[index].last_msg_;
                group_info.history_unread_msg_count = group_list_model.group_table_old_[index].history_unread_msg_count;
                group_info.history_unread_msg_count_remotely = group_list_model.group_table_old_[index].history_unread_msg_count_remotely;
                group_info.newly_create = group_list_model.group_table_old_[index].newly_create;
                --注意！！！！
                --调用im的get_group_info接口是不回返回自己在该群的屏蔽标志。它是一个通用接口，所有人可以使用
                --get_group_list是返回自己的群列表，会返回自己在群的屏蔽标志位，所以这里不更新屏蔽标志位
                group_info.mask_msg_flag = group_list_model.group_table_old_[index].mask_msg_flag;
                group_info.has_ever_got_msg = group_list_model.group_table_old_[index].has_ever_got_msg;
                if group_list_model.group_table_old_[index].b_favor ~= nil then
                    group_info.b_favor = group_list_model.group_table_old_[index].b_favor;
                end
                group_list_model.group_table_old_[index] = group_info;
                group_list_model.modify_discussion_face_url();

                group_list_model.data_refreshed_ = nil;
                group_list_model.check_list();

                --group update event
                event_center.send_event(group_event.GROUP_INFO_UPDATED,base_utility.table_to_json(group_info));
            end
        end 
    end;

    update_group_info_newim = function(group_info)
        if group_info == nil then
            return;
        end
        for index = 1, #group_list_model.group_table_new_ do
            if group_info.group_id == group_list_model.group_table_new_[index].group_id then
                group_info.last_msg_ = group_list_model.group_table_new_[index].last_msg_;
                group_info.history_unread_msg_count = group_list_model.group_table_new_[index].history_unread_msg_count;
                group_info.history_unread_msg_count_remotely = group_list_model.group_table_new_[index].history_unread_msg_count_remotely;
                group_info.newly_create = group_list_model.group_table_new_[index].newly_create;
                --注意！！！！
                --调用im的get_group_info接口是不回返回自己在该群的屏蔽标志。它是一个通用接口，所有人可以使用
                --get_group_list是返回自己的群列表，会返回自己在群的屏蔽标志位，所以这里不更新屏蔽标志位
                group_info.mask_msg_flag = group_list_model.group_table_new_[index].mask_msg_flag;
                group_info.has_ever_got_msg = group_list_model.group_table_new_[index].has_ever_got_msg;
                if group_list_model.group_table_new_[index].b_favor ~= nil then
                    group_info.b_favor = group_list_model.group_table_new_[index].b_favor;
                end
                group_list_model.group_table_new_[index] = group_info;
                group_list_model.modify_discussion_face_url();

                group_list_model.data_refreshed_ = nil;
                group_list_model.check_list();

                --group update event
                event_center.send_event(group_event.GROUP_INFO_UPDATED,base_utility.table_to_json(group_info));
            end
        end 
    end;

    check_list = function()
        for k, g_info in pairs(group_list_model.group_table_) do
            if g_info.name == nil then
            end
        end
    end;

    get_group_mask_msg_flag = function(group_id)
        local group = group_list_model.get_group_info(group_id);
        if group then
            return group["mask_msg_flag"];
        else
            return 0;
        end
    end;

    get_group_info = function(group_id)
      for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
            return group_list_model.group_table_[index];
          end
      end 
      return nil;
    end;

    get_group_show_id = function(group_id)
        local group_info = group_list_model.get_group_info(group_id);
        if group_info ~= nil then
            return group_info["group_show_id"];
        else
            return "";
        end
    end;

    get_group_info_by_index = function(index)
      return group_list_model.group_table_[index];
    end;

    --获取第index个普通群组组的信息
    get_normal_group_info_by_index = function(index)
        local loop_cnt = index;
        for i, group_info in ipairs(group_list_model.group_table_) do
            if group_info.group_type == "Public" then
                loop_cnt = loop_cnt - 1;
            end
            if loop_cnt == 0 then
                return group_info;
            end
        end
        return nil;
    end;

    on_get_remote_cfg = function(data)
        if nil == data or "table" ~= type(data) then
            return;
        end

        if data[group_list_model.MY_FAVOR_DISCUSSION_KEY] ~= nil then
            local favor_discussion_id_list_ = base_utility.json_to_table(data[group_list_model.MY_FAVOR_DISCUSSION_KEY]);
            if favor_discussion_id_list_ ~= nil then
                for index = 1, #group_list_model.group_table_ do
                    for i = 1,#favor_discussion_id_list_ do                            
                        if favor_discussion_id_list_[i] == group_list_model.group_table_[index].group_id then
                            group_list_model.group_table_[index].b_favor = 1;
                            group_list_model.data_refreshed_ = nil;
                            break;
                        end
                    end
                end
                for index = 1, #group_list_model.group_table_old_ do
                    for i = 1,#favor_discussion_id_list_ do                            
                        if favor_discussion_id_list_[i] == group_list_model.group_table_old_[index].group_id then
                            group_list_model.group_table_old_[index].b_favor = 1;
                            group_list_model.data_refreshed_ = nil;
                            break;
                        end
                    end
                end
                for index = 1, #group_list_model.group_table_new_ do
                    for i = 1,#favor_discussion_id_list_ do                            
                        if favor_discussion_id_list_[i] == group_list_model.group_table_new_[index].group_id then
                            group_list_model.group_table_new_[index].b_favor = 1;
                            group_list_model.data_refreshed_ = nil;
                            break;
                        end
                    end
                end
                event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}"); 
            end
        end
    end;

    get_favor_groupchat_flag = function(data)
        local out_data = data;
        out_data.b_favor = 0;
        for i, group_info in ipairs(group_list_model.group_table_) do
            if out_data.group_id == group_info.group_id then
                if group_info.b_favor ~= nil then
                  out_data.b_favor = group_info.b_favor;
                end
                break;
            end
        end
        return out_data;
    end;

    set_favor_groupchat_flag = function(data)
        for i, group_info in ipairs(group_list_model.group_table_old_) do
            if data.group_id == group_info.group_id then
                group_list_model.group_table_old_[i].b_favor = data.b_favor;
                break;
            end
        end
        for i, group_info in ipairs(group_list_model.group_table_new_) do
            if data.group_id == group_info.group_id then
                group_list_model.group_table_new_[i].b_favor = data.b_favor;
                break;
            end
        end
        group_list_model.gen_group_list();
        group_list_model.data_refreshed_ = nil;
        group_list_model.favor_group_list_changed();
    end;

    favor_group_list_changed = function()
        local fave_g_list = {};
        for i, group_info in ipairs(group_list_model.group_table_) do
            if group_info.b_favor ~= nil and group_info.b_favor == 1 then
                table.insert(fave_g_list, group_info.group_id);
            end
        end

        social_remote_cfg_mgr.save_one_key(group_list_model.MY_FAVOR_DISCUSSION_KEY, base_utility.table_to_json(fave_g_list), true);
        event_center.send_event(social_event.SOCIAL_GROUP_COUNT_CHANGE, "{}"); 
    end;

    --获取第index个收藏的讨论组的信息
    get_favor_discussion_group_info_by_index = function(index)
        local loop_cnt = index;
        for i, group_info in ipairs(group_list_model.group_table_) do
            if group_info.group_type == "MobileDiscus" --[[and group_info.b_favor ~= nil and group_info.b_favor == 1]] then
                loop_cnt = loop_cnt - 1;
            end
            if loop_cnt == 0 then
                return group_info;
            end
        end
        return nil;
    end;

    --获取收藏讨论组和群组的个数
    get_valid_group_count = function()
        local discus_cnt = 0; -- 讨论组
        local public_cnt = 0; -- 群组
        for i, group_info in ipairs(group_list_model.group_table_) do
            if group_info.group_type == "MobileDiscus" --[[and group_info.b_favor ~= nil and group_info.b_favor == 1]] then
                discus_cnt = discus_cnt + 1;
            elseif group_info.group_type == "Public" then
                public_cnt = public_cnt + 1;
            end
        end
        return discus_cnt, public_cnt;
    end;

    get_group_count = function()
      return #group_list_model.group_table_;
    end;

    get_group_count_ext = function()
      local cnt = 0;
      for index, group_info in ipairs(group_list_model.group_table_) do
          if group_info.group_type == "Public" then
              cnt = cnt + 1;
          end
      end
      return cnt;
    end;

    get_favor_discussion_count = function()
      local cnt = 0;
      for index, group_info in ipairs(group_list_model.group_table_) do
          if group_info.group_type == "MobileDiscus" 
             --[[and group_info.b_favor ~= nil and group_info.b_favor == 1]] then

              cnt = cnt + 1;
          end
      end
      return cnt;
    end;

    update_group_last_message_time = function(group_id, time, time_ymd, time_hms)
        for index = 1, #group_list_model.group_table_ do
            if group_id == group_list_model.group_table_[index].group_id then
                group_list_model.group_table_[index]["last_msg_time"] = time;
                group_list_model.group_table_[index]["last_msg_time_ymd"] = time_ymd;
                group_list_model.group_table_[index]["last_msg_time_hms"] = time_hms;
                group_list_model.data_refreshed_ = nil;
                group_list_model.check_list();
              break;
            end
        end
    end;

    get_total_unread_msg_count = function ()
        local count = 0;
        for index = 1, #group_list_model.group_table_ do
            if group_list_model.group_table_[index].history_unread_msg_count then
                count = count + group_list_model.group_table_[index].history_unread_msg_count;
            end;
        end

        return count;
    end;

    set_history_unread_msg_count = function(group_id, count)
        for index = 1, #group_list_model.group_table_ do
            if group_id == group_list_model.group_table_[index].group_id then
                group_list_model.group_table_[index].history_unread_msg_count = count;
                group_list_model.data_refreshed_ = nil;
                break;
            end
        end

        comm_center.send_broadcast('i_broadcast_social_group_on_new_msg', {});
    end;

    clear_history_unread_msg_count = function(group_id)
        for index = 1, #group_list_model.group_table_ do
            if group_id == group_list_model.group_table_[index].group_id then
                group_list_model.group_table_[index].history_unread_msg_count = 0;
                group_list_model.data_refreshed_ = nil;
                break;
            end
        end    
    end;

    get_group_history_unread_msg_count = function(group_id)
        local group = group_list_model.get_group_info(group_id);
        if group == nil or group.history_unread_msg_count == nil then
            return 0;
        else
            return group.history_unread_msg_count;
        end
    end;

    set_history_unread_msg_count_remotely = function(group_id, count)
        for index = 1, #group_list_model.group_table_ do
            if group_id == group_list_model.group_table_[index].group_id then
                group_list_model.group_table_[index].history_unread_msg_count_remotely = count;
                group_list_model.data_refreshed_ = nil;
                break;
            end
        end
    end;

    clear_history_unread_msg_count_remotely = function(group_id)
        for index = 1, #group_list_model.group_table_ do
            if group_id == group_list_model.group_table_[index].group_id then
                group_list_model.group_table_[index].history_unread_msg_count_remotely = 0;
                group_list_model.data_refreshed_ = nil;
                break;
            end
        end    
    end;

    get_group_history_unread_msg_count_remotely = function(group_id)
        local group = group_list_model.get_group_info(group_id);
        if group == nil or group.history_unread_msg_count_remotely == nil then
            return 0;
        else
            return group.history_unread_msg_count_remotely;
        end
    end;

    set_last_msg = function(group_id, msg)
        for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
              group_list_model.group_table_[index].last_msg_ = msg;
              group_list_model.data_refreshed_ = nil;
              group_list_model.check_list();
              break;
          end
        end

        local groups = group_list_model.get_group_list();
        local group_list = {};
        for i_group = 1, #groups do
            table.insert(group_list, groups[i_group].group_id);
        end

        local group_list_param = {};
        group_list_param.id_list = group_list;

        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();");
        social_network:get_group_unread_msg_num(base_utility.table_to_json(group_list_param));
    end;

    on_broadcast_on_last_msg = function(param_in)
        group_list_model.set_last_msg(param_in.group_id, param_in.msg);
    end;

    get_last_msg = function(group_id)
        for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
              return group_list_model.group_table_[index].last_msg_;
          end
        end
    end;   

    get_last_msg_time = function(group_id)
        for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
              if group_list_model.group_table_[index].last_msg_ == nil then
                  return 0;
              else
                  return group_list_model.group_table_[index].last_msg_.time;
              end
          end
        end

        return 0;
    end;       

    set_has_ever_got_msg = function(group_id, has_ever_got_msg)
        for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
              group_list_model.group_table_[index].has_ever_got_msg = has_ever_got_msg;
              group_list_model.data_refreshed_ = nil;
              break;
          end
        end
    end;

    get_has_ever_got_msg = function(group_id)
        for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
              if group_list_model.group_table_[index].has_ever_got_msg == nil then
                  return false;
              else
                  return group_list_model.group_table_[index].has_ever_got_msg;
              end
          end
        end

        return false;
    end;    

    set_recommend_group = function(groups)
        group_list_model.recommend_group = groups;
        group_list_model.data_refreshed_ = nil;
        event_center.send_event(social_event.GET_RECOMMENT_GROUP_SUCCESS, "{}");

    end; 

    get_recommend_group = function(groups)
        return group_list_model.recommend_group;
    end; 

    get_group_type = function(group_id)
        for index = 1, #group_list_model.group_table_ do
          if group_id == group_list_model.group_table_[index].group_id then
              return group_list_model.group_table_[index].group_type;
          end
        end
        return "";
    end;   

    get_match_room_info = function(room_id)
        for index = 1, #group_list_model.group_table_ do
            if group_list_model.group_table_[index].group_type == "MatchLiveRoom" then
                local ext_info = base_utility.json_to_table(group_list_model.group_table_[index].ext_data2);
                if ext_info ~= nil and ext_info.room_id ~= nil and ext_info.room_id == room_id then
                    return group_list_model.group_table_[index];
                end
            end
        end
        return nil;
    end;   

    get_common_room_group_info = function(room_id)
        for index = 1, #group_list_model.group_table_ do
            if group_list_model.group_table_[index].group_type == "CommonTopicRoom" then
                local ext_info = base_utility.json_to_table(group_list_model.group_table_[index].ext_data2);
                if ext_info ~= nil and ext_info.room_id ~= nil and ext_info.room_id == room_id then
                    return group_list_model.group_table_[index];
                end
            end
        end
        return nil;
    end;
};

group_list_model.init();
